package com.ib.algokit;

import java.util.Properties;

import org.activequant.container.ITradeSystemContext;
import org.activequant.dao.hibernate.config.HibernateDaoConfig;
import org.activequant.dao.hibernate.config.LocalSessionFactoryConfig;
import org.activequant.tradesystem.ib.LiveContextConfig;
import org.activequant.tradesystem.ib.TradeProxyConfig;
import org.activequant.tradesystem.types.Mode;
import org.springframework.config.java.context.JavaConfigApplicationContext;
import org.springframework.context.ApplicationContext;


/**
 * Main class to build trading system from Config blocks, and run it.
 * <p>
 * This class implements a simple container for trading system, using 
 * Spring JavaConfig application context.
 * <p>
 * There have to be the following beans defined by the configurations:
 * <ul>
 *    <li><code>system</code> is the trading system itself. Depending on what 
 *        trading system needs, it may implement none, one, all, or some of the
 *        following interfaces: {@link ILifeCycleAware}, {@link IBrokerAware},
 *        {@link IQuoteSubscriptionAware}, {@link ITradeIndicationSubscriptionAware},
 *        {@link ICandleSubscriptionAware}.
 *    <li><code>main</code> is a bean implementing <code>Runnable</code> interface. 
 *        This is the main "runner" code: container will run this code, 
 *        and when it exits, container will stop.
 *    <li>
 * </ul>
 * Other beans are optional, and may be needed if tradesystem requests a specific
 * service. For example, implementing {@link IBrokerAware} interface tradesystem
 * asks for a broker interface to be injected, and therefore, "broker" bean
 * has to be defined in the application context. 
 * <p>
 *
 *  @author Dimitar Misev
 */
public class RunMe {
	
	public static boolean isMarketDepthEnabled, isBehindIBProxy;
//	public static boolean isRealRun;
	public static Mode mode = Mode.LIVE;
	
//	public static int numberOfBackTestLoops;
//	public static int currentBackTestIteration;
	public static void main(String[] args) throws Exception {
		
//		isBehindIBProxy=false;//true
		
		isBehindIBProxy = mode ==Mode.LIVE;
		
		isMarketDepthEnabled=false;//true
		if(isBehindIBProxy){
			Properties props = System.getProperties();
			props.put("http.proxyHost", "192.168.133.254");
			props.put("http.proxyPort", "3128");
		}
		
		if(mode == Mode.LIVE){
			ApplicationContext factory = new JavaConfigApplicationContext(
					// backtester that uses CSV historical quotes  
					//BacktestCsvContextConfig.class,
					// live runner that uses IB broker and IB quote feed.
					LiveContextConfig.class,					
					// backtester that reads quote data from the database
					//BacktestDaoContextConfig.class,
					HibernateDaoConfig.class,
					LocalSessionFactoryConfig.class,
					// trading system itself
					TradeProxyConfig.class
				);
			ITradeSystemContext context = (ITradeSystemContext) factory.getBean("context");
			Object system = factory.getBean("system");
			context.add(system);
			context.start();
		}else{
			

				ApplicationContext factory = new JavaConfigApplicationContext(					
						// backtester that reads quote data from the database
						BacktestDaoContextConfig.class,
						HibernateDaoConfig.class,
						LocalSessionFactoryConfig.class,
						// trading system itself
						TradeProxyConfig.class
					);
				ITradeSystemContext context = (ITradeSystemContext) factory.getBean("context");
				Object system = factory.getBean("system");
				context.add(system);
				context.start();

		}
	}
}